var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var do_DataCache = sm("do_DataCache");
var deviceone = require("deviceone");
var communityUrl = require("url/community");
var communityUtil = require("helper/community");
var qqHttp = require("helper/http");
var listdataNotices = mm("do_ListData");
var helperOpen = require("helper/open");
var noData = [{"template":1}];
// 本页面-ajax业务请求url
var httpUrl = communityUrl.url.notice.page;

// UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");
// 在aLayoutRoot上动态添加子视图(用于等待数据装载的过程)
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
var listView = ui("listView");
// 定义变量
var page = 1;
var type_id = "news_list";
var type;

// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
headerRight.visible = false;
headerLeft.on("touch", function() {
	do_App.closePage()
});
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "公告"
})
header.refreshData();
// 页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	// 读取当前页面的传入参数
	laodPage();
});
listView.bindItems(listdataNotices);
// 刷新数据
// 刷新数据
function laodPage() {
	page = 1;
	var params = {
		page : page,
		communityId : communityUtil.communityVisitId()
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdataNotices.removeAll();
		if(data.page.content.length){
			listdataNotices.addData(data.page.content);
		}
		else{
			listdataNotices.addData(noData);
		}
		listView.refreshItems();
		do_DataCache.saveData(type_id, data);
	})
}
// 刷新数据
function getNextPageData() {
	page++;
	var params = {
		page : page,
		communityId : communityUtil.communityVisitId()
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdataNotices.addData(data.page.content);
		listView.refreshItems();
	})
}

// 下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		laodPage();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
	}
});
do_Page.on("result", function(data) {
	// 更新远程最新数据
	laodPage();
});
//点击一条新闻
listView.on("touch", function(data) {
	var onNotice = listdataNotices.getOne(data);
	helperOpen.open("source://view/community/notice/detail/main.ui",{id:onNotice.id});
});
